package com.zds.passwordbox.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.DialogFragment;

import com.zds.passwordbox.R;
import com.zds.passwordbox.common.Constant;
import com.zds.passwordbox.data.dao.PrivateKeyDao;

public class PrivateKeyDialog extends DialogFragment {

    private final PrivateKeyDao instance;

    private boolean isHidden = true;

    private SharedPreferences sharedPreferences;

    public PrivateKeyDialog(PrivateKeyDao instance) {
        this.instance = instance;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction.
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater.
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.private_key_dialog, null);
        EditText editText = view.findViewById(R.id.private_user_key);
        setLeftDrawable(editText, ContextCompat.getDrawable(requireContext(), R.mipmap.private_key));
        ImageView showHideToggleButton = view.findViewById(R.id.showHideToggleButton1);
        showHideToggleButton.setOnClickListener(v -> {
            isHidden = !isHidden;
            if (isHidden) {
                editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
            } else {
                editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
            editText.setSelection(editText.getText().length());
        });
        // Inflate and set the layout for the dialog.
        // Pass null as the parent view because it's going in the dialog layout.
        builder.setView(view)
                .setTitle("个人密钥设置")
                .setNegativeButton("取消", (dialog, id) -> {
                    dialog.cancel();
                });
        if (!instance.isUserKeyExist()) {
            builder.setPositiveButton("确定", (dialog, id) -> {
                if (editText != null) {
                    Editable text = editText.getText();
                    System.out.println(text.toString());
                    if (instance.isUserKeyExist()) {
                        dialog.dismiss();
                    }
                    instance.saveUserKey(text.toString());
                    if (sharedPreferences == null) {
                        sharedPreferences = requireActivity().getSharedPreferences(Constant.USER_SETTING_NAME,
                                Context.MODE_PRIVATE);
                    }
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    edit.putBoolean(Constant.USER_KEY, true);
                    edit.apply();
                }
            });
        } else {
            editText.setText(instance.getUserKey());
        }


        // Create the AlertDialog object and return it.
        return builder.create();
    }

    private void setLeftDrawable(EditText editText, Drawable drawable) {
        if (drawable == null) return;
        drawable.setBounds(0, 0, 70, 70);
        editText.setCompoundDrawables(drawable, null, null, null);
    }
}
